package com.it2297.model;

import java.sql.PreparedStatement;

import java.sql.ResultSet;
import java.sql.SQLException;


import com.it2297.main.App;


public class Customer extends User {
	private int loyaltyPoints;
	
	public int getLoyaltyPoints(){
	return loyaltyPoints;
	}
	
	public void setLoyaltyPoints(int loyaltyPoints){
		this.loyaltyPoints = loyaltyPoints;
	}

	public Customer getSingle(String NRIC){
		try{
			App.mySqlConnection.open();
			
			PreparedStatement preparedStatement = App.mySqlConnection.getConnection().prepareStatement("SELECT * FROM americano.CUSTOMER_VIEW WHERE nric = ?");
			preparedStatement.setString(1, NRIC);
			ResultSet resultSet = preparedStatement.executeQuery();

			if (resultSet.next()) {
				this.setAttributes(resultSet);
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}
		
		return this;
	}
	
	private void setAttributes(ResultSet resultSet){
		try{
			this.setLoyaltyPoints(resultSet.getInt("loyalty_point"));
			this.setAddress(resultSet.getString("address"));
			this.setCountryOfBirth(resultSet.getString("birth_country"));
			this.setDateRegister(resultSet.getTimestamp("date_register"));
			this.setDob(resultSet.getDate("dob"));
			this.setEmail(resultSet.getString("email"));
			this.setEthnic(resultSet.getString("ethnic"));
			this.setGender(resultSet.getString("gender"));
			this.setName(resultSet.getString("name"));
			this.setNric(resultSet.getString("nric"));
			this.setPhone(resultSet.getString("phone"));
			this.setStatus(resultSet.getString("status"));
			
		}catch (SQLException ex) {
			ex.printStackTrace();
		}	}
}